﻿@model CustomerAddressEditModel
@using SpotsHub.Gateway.Models.Customer;
@{
    Layout = "~/Views/Shared/_Layout.cshtml";

    //title
    Html.AddTitleParts(T("PageTitle.Account").Text);
}

@using (Html.BeginForm())
{
    <div class="page account-page address-edit-page customer-info-page">
        <div class="window">
        
        <div class="window-title grad-lightgrey grad">
            <div class="window-title-bar grad-titleshine grad">
                <h2 class="page-title"> @T("Account.CustomerAddresses.AddNew")</h2>
            </div>
        </div>
        <div class="window-content space">

            @{
    var dataDictAddress = new ViewDataDictionary();
    //Merge ModelState (required for validation)
    dataDictAddress.ModelState.Merge(ViewData.ModelState);
    dataDictAddress.TemplateInfo.HtmlFieldPrefix = "Address";
           
            }

                        @Html.ValidationSummary(false)
            @Html.HiddenFor(model => Model.Address.Id)
            <div class="window-left">
                <div class="info-row">
                    <div class="info-row-left">
                        <div class="info-label">
                            @Html.RequiredHint()
                            @Html.LabelFor(model => Model.Address.FirstName)
                        </div>
                        <div class="info-input">
                            @Html.TextBoxFor(model => Model.Address.FirstName, new { id = "FirstName-" + Model.Address.Id.ToString()})
                        </div>
                    </div>
                    <div class="info-row-right">
                        <div class="info-label">
                            @Html.RequiredHint()
                            @Html.LabelFor(model => Model.Address.LastName)
                        </div>
                        <div class="info-input">
                            @Html.TextBoxFor(model => Model.Address.LastName, new { id = "LastName-" + Model.Address.Id.ToString()})
                        </div>
                    </div>
                </div>
                @if (Model.Address.CompanyEnabled)
                {
                    <div class="info-row">
                        <div class="info-label">
                            @if (Model.Address.CompanyRequired)
                            {
                                @Html.RequiredHint()
                            }
                            @Html.LabelFor(model => Model.Address.Company)
                        </div>
                        <div class="info-input">
                            @Html.TextBoxFor(model => Model.Address.Company, new { id = "Company-" + Model.Address.Id.ToString()})
                        </div>
                    </div>
                }
                <div class="info-row">
                    <div class="info-label">
                        @Html.RequiredHint()
                        @Html.LabelFor(model => Model.Address.Email)
                    </div>
                    <div class="info-input">
                        @Html.TextBoxFor(model => Model.Address.Email, new { id = "Email-" + Model.Address.Id.ToString()})
                    </div>
                </div>
                @if (Model.Address.PhoneEnabled)
                {
                    <div class="info-row">
                        <div class="info-row-left">
                            <div class="info-label">
                                @if (Model.Address.CellPhoneRequired)
                                {
                                    @Html.RequiredHint()
                                }
                                @Html.LabelFor(model => Model.Address.CellPhoneNumber)
                            </div>
                            <div class="info-input">
                                @Html.TextBoxFor(model => Model.Address.CellPhoneNumber, new { id = "CellPhoneNumber-" + Model.Address.Id.ToString()})
                            </div>
                        </div>
                        <div class="info-row-right">
                            <div class="info-label">
                                @Html.LabelFor(model => Model.Address.PhoneNumber)
                            </div>
                            <div class="info-input">
                                @Html.TextBoxFor(model => Model.Address.PhoneNumber, new { id = "PhoneNumber-" + Model.Address.Id.ToString()})
                            </div>
                        </div>
                    </div>
                }
                                            
            </div>
            <div class="window-right">
            @if (Model.Address.StreetAddressEnabled)
            {
                <div class="info-row">
                    <div class="info-label">
                        @if (Model.Address.StreetAddressRequired)
                        {
                            @Html.RequiredHint()
                        }
                        @Html.LabelFor(model => Model.Address.Address1)
                    </div>
                    <div class="info-input">
                        @Html.TextBoxFor(model => Model.Address.Address1, new { id = "Address1-" + Model.Address.Id.ToString()})
                    </div>
                </div>
            }
            @if (Model.Address.StreetAddress2Enabled)
            {
                <div class="info-row">
                    <div class="info-label">
                        @Html.LabelFor(model => Model.Address.Address2)
                    </div>
                    <div class="info-input">
                        @Html.TextBoxFor(model => Model.Address.Address2, new { id = "Address2-" + Model.Address.Id.ToString()})
                    </div>
                </div>
            }

                @if (Model.Address.ZipPostalCodeEnabled || Model.Address.CityEnabled)
                {
                    <div class="info-row">
                        @if (Model.Address.ZipPostalCodeEnabled)
                        {
                            <div class="info-row-left">
                                <div class="info-label">
                                    @if (Model.Address.ZipPostalCodeRequired)
                                    {
                                        @Html.RequiredHint()
                                    }
                                    @Html.LabelFor(model => Model.Address.ZipPostalCode)
                                </div>
                                <div class="info-input">
                                    @Html.EditorFor(model => Model.Address.ZipPostalCode, new { id = "ZipPostalCode-" + Model.Address.Id.ToString()})
                                </div>
                                @Html.ValidationMessageFor(model => Model.Address.ZipPostalCode)
                            </div>
                        }
                        @if (Model.Address.CityEnabled)
                        {
                            <div class="info-row-right">
                                <div class="info-label">
                                    @if (Model.Address.CityRequired)
                                    {
                                        @Html.RequiredHint()
                                    }
                                    @Html.LabelFor(model => Model.Address.City)
                                </div>
                                <div class="info-input">
                                    @Html.EditorFor(model => Model.Address.City, new { id = "City-" + Model.Address.Id.ToString()})
                                </div>
                                @Html.ValidationMessageFor(model => Model.Address.City)
                            </div>

                        }
                    </div>
                    }
                    @if (Model.Address.CountryEnabled && Model.Address.StateProvinceEnabled)
                    {
                    <div class="info-row">
                        @if (Model.Address.CountryEnabled)
                        {
                            <div class="info-row-left">
                                <div class="info-label">
                                    @Html.LabelFor(model => Model.Address.CountryId)
                                </div>
                                <div class="info-input">
                                    @Html.DropDownListFor(model => Model.Address.CountryId, Model.Address.AvailableCountries, new { id = "CountryId-" + Model.Address.Id.ToString()})
                                </div>
                            </div>

                        }
                        @if (Model.Address.CountryEnabled && Model.Address.StateProvinceEnabled)
                        {
                            <div class="info-row-right">
                                <div class="info-label">
                                    @Html.LabelFor(model => Model.Address.StateProvinceId)
                                </div>
                                <div class="info-input">
                                    @Html.DropDownListFor(model => Model.Address.StateProvinceId, Model.Address.AvailableStates, new { id = "StateProvinceId-" + Model.Address.Id.ToString()})
                                </div>
                            </div>
                        }
                    </div>
                }
            </div>
            <div class="content-clear"></div>
            </div>
            <div class="window-content space">
            <div class="buttons">
                <input type="submit" class="button-red save-address-button" value="@T("Common.Save")" />
            </div>
            </div>
        </div>
        <script type="text/javascript">
            $("select[id*=CountryId-]").live("change", function () {
                var selectedItem = $(this).val();
                var ddlStates = $(this).closest('form').find('[id*=StateProvinceId-]');

                $.ajax({
                    cache: false,
                    type: "GET",
                    url: "@(Url.RouteUrl("GetStatesByCountryId"))",
                        data: { "countryId": selectedItem, "addEmptyStateIfRequired": "true" },
                        success: function (data) {
                            ddlStates.html('');
                            $.each(data, function (id, option) {
                                ddlStates.append($('<option></option>').val(option.id).html(option.name));
                            });
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert('Failed to retrieve states.');
                        }
                    });
            });
        </script>
    </div>
}

